$(function (){
    $("#mobileError").hide();
    $("#passwordError").hide();
    $("#passwordSureError").hide();
    $("#checkCodeError").hide();
    $("#emailError").hide();
    $("#passwordErrorEmail").hide();
    $("#passwordSureErrorEmail").hide();
    $("#checkCodeErrorEmail").hide();
})

// 校验手机号码
function checkMobile(){
    var mobile = $("#mobile").val();
    if(mobile == null || mobile == ""){
        $("#mobileError").text("请输入正确的手机号！");
        $("#mobileError").show();
        return false;
    }
    var mobileReg = /^1\d{10}$/;
    if(!mobileReg.test(mobile)){
        $("#mobileError").text("请输入正确的手机号！");
        $("#mobileError").show();
        return false;
    }
    $("#mobileError").hide();
    return true;
}

// 校验邮箱
function checkEmail(){
    var email = $("#email").val();
    if(email == null || email == ""){
        $("#emailError").text("请输入正确的邮箱！");
        $("#emailError").show();
        return false;
    }
    var reg = /^\w+[@]\w{2,5}([.]\w{2,3}){1,3}$/i;
    if(!reg.test(email)){
        $("#emailError").text("请输入正确的邮箱！");
        $("#emailError").show();
        return false;
    }
    $("#emailError").hide();
    return true;
}

// 校验密码
function checkPassword(type){
    var password = type == 1 ? $("#password").val() : $("#passwordEmail").val();
    var elementObj = type == 1 ? $("#passwordError") : $("#passwordErrorEmail");
    if(password == null || password == ""){
        elementObj.text("密码长度在8到32之间！");
        elementObj.show();
        return false;
    }
    var reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\d)(?=.*[$@$!%*?&.])[A-Za-z\d$@$!%*?&]{8,32}/;
    // if(reg.test(password)){
    //     elementObj.text("密码提示：输入密码应当是8-32位字符，包含数字、小写、大写");
    //     elementObj.show();
    //     return false;
    // }
    elementObj.hide();
    return true;
}

// 校验确认密码
function checkPasswordSure(type){
    var password = type == 1 ? $("#password").val() : $("#passwordEmail").val();
    var passwordSure = type == 1 ? $("#passwordSure").val() : $("#passwordSureEmail").val();
    var elementObj = type == 1 ? $("#passwordSureError") : $("#passwordSureErrorEmail");

    if(passwordSure == null || passwordSure == ""){
        elementObj.text("请输入确认密码！");
        elementObj.show();
        return false;
    }
    if(passwordSure != password){
        elementObj.text("两次密码输入不一致！");
        elementObj.show();
        return false;
    }
    elementObj.hide();
    return true;
}

// 发送验证码
function sendCheckCode(type){
    // 校验手机号码
    if(type == 1 && !checkMobile()){
        return false;
    }
    // 校验邮箱
    if(type == 2 && !checkEmail()){
        return false;
    }
    if(!checkPassword(type)){
        return false;
    }
    if(!checkPasswordSure(type)){
        return false;
    }
    var elementObj = type == 1 ? $("#checkCodeBtnMobile") : $("#checkCodeBtnEmail");
    var timer = setInterval(beginTime,1000);
    var countdown = 60;
    function beginTime(){
        elementObj.text(countdown + "秒后获取");
        if (countdown == 1) {
            elementObj.text("获取验证码");
            //进度百分百后，清除掉定时器
            clearInterval(timer);
            return false;
        } else {
            if (countdown == 60) {
                // 请求地址
                var url = type == 1 ? '/user-center-oauth/public/sms/captcha/send' : "/user-center-oauth/public/email/captcha/send";
                var account = type == 1 ? $("#mobile").val() : $("#email").val();
                var data = type == 1 ? {phone: account} : {email: account};
                $.ajax({
                    url: url,
                    type: "post",
                    data: JSON.stringify(data),
                    headers: {'Content-Type': 'application/json'},
                    dataType: "json",
                    success: function (data) {
                        if (data != "") {
                            if (data.code == 200) {
                                alert("验证码发送成功");
                            } else {
                                alert(data.msg);
                                elementObj.text("获取验证码");
                                //进度百分百后，清除掉定时器
                                clearInterval(timer);
                            }
                        }
                    }
                });
            }
            countdown--;
        }
    }
}

// 注册
function registerNext(type){
    // 校验手机号码
    if(type == 1 && !checkMobile()){
        return false;
    }
    // 校验邮箱
    if(type == 2 && !checkEmail()){
        return false;
    }
    if(!checkPassword(type)){
        return false;
    }
    if(!checkPasswordSure(type)){
        return false;
    }
    // 判断验证码是否输入
    var checkCode = type == 1 ? $("#checkCodeMobile").val() : $("#checkCodeEmail").val();
    var elementObj = type == 1 ? $("#checkCodeError") : $("#checkCodeErrorEmail");
    if(checkCode == null || checkCode == ""){
        elementObj.text("请输入验证码！");
        elementObj.show();
        return false;
    }
    elementObj.hide();

    var account = type == 1 ? $("#mobile").val() : $("#email").val();
    var password = type == 1 ? $("#password").val() : $("#passwordEmail").val();
    var passwordEn = encrypt(password, "e0da368a8fa3d9b3f691e82957ec5b75").toUpperCase();
    var data = {passwd: passwordEn, confirmPasswd: passwordEn, captcha: checkCode};
    if(type == 1){
        data.phone = account;
    }else {
        data.email = account;
    }
    var url = type == 1 ? "/user-center-oauth/public/sms/user/register-valid" : "/user-center-oauth/public/email/user/register-valid";
    $.ajax({
        url: url,
        type: "post",
        data: JSON.stringify(data),
        headers: { 'Content-Type': 'application/json'},
        dataType: "json",
        success: function (data) {
            if (data != "") {
                if(data.code == 200){
                    window.location.href="registerNext.html";
                }else {
                    alert(data.msg);
                }
            }
        }
    });
}

